<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/21
 * Time: 20:29
 */

namespace app\lib;


use yii\web\Controller;
use Yii;
use app\lib\WeixinUtils;

class WXBaseController extends Controller
{
    Const  sessionKey = 'user_info';
    public $enableCsrfValidation = false;
    public $layout = false;

    public function init()
    {

    }

    public static function getSessionUserid()
    {
        $session = Yii::$app->session;
        return $session->get(static::sessionKey)->userid;
    }

    public static function getSessionOpenid()
    {
        $session = Yii::$app->session;
        return $session->get(static::sessionKey)->openid;
    }

    public static function getRequestPost($key)
    {
        $request = Yii::$app->request;
        return $request->post($key);
    }

    public static function __validUser($isjson = false)
    {
        $userinfo = WeixinUtils::weixin_userinfo_filter();
        if ($userinfo == null) {
            $msg = "验证失败";
            if ($isjson) {
                echo json_encode(['msg' => $msg, 'flag' => 0,]);
            } else {
                echo $msg;
            }
            exit;
        }
    }

    public static function dump_info($info = '', $title = '提示')
    {

        echo Yii::$app->getView()->renderPhpFile('../views/wx-home/info.php', ['info' => $info, 'title' => $title]);
        exit;
    }

    public static function var_dump_out($var)
    {
        ob_start();
        var_dump($var);
        $a = ob_get_contents();
        ob_end_clean();
        return $a;
        return htmlspecialchars($a, ENT_QUOTES); // Escape every HTML special chars (especially > and < )
    }

    public static function isPost()
    {
        return Yii::$app->request->isPost;
    }

    public static function isGet()
    {
        return Yii::$app->request->isGet;
    }

    public static function returnAjax($data)
    {
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;

    }

    public static function getSessionValue($key)
    {
        $session = Yii::$app->session;
        return $session->get($key);
    }

    public static function removeSessionValue($key)
    {
        $session = Yii::$app->session;
        return $session->remove($key);
    }

    public static function setSessionValue($key, $value)
    {
        $session = Yii::$app->session;
        $session->set($key, $value);
    }
    public function renderJson($data, $status = 0, $message = 'success')
    {
        $result = [
            'status' => $status,
            'message' => $message,
            'data' => $data
        ];

        return json_encode($result, true);
    }
}